#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <set>
#include <map> 

#define flr(x, l, r) for (int x = l; x <= r; ++x)
#define frl(x, r, l) for (int x = r; x >= l; --x)
#define fmp(x, t) for (int x = h[t]; ~x; x = ne[x])
#define LL long long
#define mt memset
#define my memcpy
#define szf sizeof
#define INF 0x3f3f3f3f
#define in(x) scanf("%d", &x)
#define out(x) printf("%d", x)
#define inll(x) scanf("%lld", &x)
#define outll(x) printf("%lld", x)
#define pn printf("\n")
#define con continue
#define bk break
#define vc vector
#define pb push_back
#define sz size
#define PII pair<int, int>
#define x first
#define y second
#define P_q priority_queue
#define ft front
#define pf push_front
#define popf pop_front
#define it insert
#define ct count

using namespace std;

const int N = 1010, M = N * N, K = 2 * M;

int n, m, k;
struct Edge {
    int a, b, w;
}e[K];
int p[M];
int ids[N][N];

int find(int x) {
    if (p[x] != x) p[x] = find(p[x]);
    return p[x];
}

void get_edges() {
    int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}, dw[4] = {1, 2, 1, 2};
    
    flr (z, 0, 1)
        flr (i, 1, n)
            flr (j, 1, m)
                flr (u, 0, 3)
                    if (u % 2 == z) {
                        int x = i + dx[u], y = j + dy[u], w = dw[u];
                        if (x && x <= n && y && y <= m) {
                            int a = ids[i][j], b = ids[x][y];
                            if (a < b) e[k ++ ] = {a, b, w};
                        }
                    }
}

int main() {
	in(n), in(m);
	
	int t = 1;
	flr (i, 1, n)
	    flr (j, 1, m)
	        ids[i][j] = t ++ ;
	
	flr (i, 1, n * m) p[i] = i;
	
	int x1, y1, x2, y2;
	while (cin >> x1 >> y1 >> x2 >> y2) {
	    int a = ids[x1][y1], b = ids[x2][y2];
	    p[find(a)] = find(b);
	}
    
    get_edges();
    
    int res = 0;
    flr (i, 0, k - 1) {
        int a = find(e[i].a), b = find(e[i].b), w = e[i].w;
        if (a != b) {
            p[a] = b;
            res += w;
        }
    }
    
    out(res), pn;
    
    return 0;
}